package be.phl.obstacleapp;

import be.phl.animation.ActivitySwitcher;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import be.phl.obstacleapp.*;

public class MainActivity extends Activity {

	// protected static final String AnimationUtils = null;
	private int width, height;
	
	

	// private Animation mRotateAnimation = null;
	public int getWidth() {
		return width; // 1280 screen resolution in pixels
	}

	public int getHeight() {
		return height; // 800
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getScreenDimensions();
		// mRotateAnimation = AnimationUtils.loadAnimation(this, );
		LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		int height = getHeight();
		int width = getWidth();

		int heightButton = getHeight() / 100 * 70;
		final float scale = getResources().getDisplayMetrics().density;
		// int heightButton=(int) (33 * scale + 0.5f);
		p.height = heightButton;
		p.width = (getWidth() / 5) - ((getWidth() / 5) / 10);
		p.setMargins(getWidth() / 200, (getHeight() - heightButton) / 7,
				getWidth() / 200, getWidth() / 100); // (getWidth()/5)/10)/2

		ImageButton btnNewScreening = (ImageButton) findViewById(R.id.imageButtonNewScreening);
		btnNewScreening.setLayoutParams(new LinearLayout.LayoutParams(p));
		// btnScreeningsOverview.setScaleType();
		btnNewScreening.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageButton btnNewScreening = (ImageButton) findViewById(R.id.imageButtonNewScreening);

				Animation rotate1 = AnimationUtils.loadAnimation(
						MainActivity.this, R.animator.wave_scale);

				rotate1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						final Intent openNewScreening = new Intent(
								MainActivity.this, PatientsActivity.class);
						openNewScreening.putExtra("Screen", 2);
						startActivity(openNewScreening);
						overridePendingTransition(R.animator.toright, R.animator.toleft);
						// openNewScreening
						// .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// ActivitySwitcher
						// .animationOut(
						// findViewById(R.id.Totaal),
						// getWindowManager(),
						// new ActivitySwitcher.AnimationFinishedListener() {
						//
						// @Override
						// public void onAnimationFinished() {
						// MainActivity.this
						// .startActivity(openNewScreening);
						// }
						//
						// });
					}
				});
				btnNewScreening.startAnimation(rotate1);
			}
		});

		ImageButton btnPatients = (ImageButton) findViewById(R.id.imageButtonPatients);
		btnPatients.setLayoutParams(new LinearLayout.LayoutParams(p));
		btnPatients.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageButton btnPatients = (ImageButton) findViewById(R.id.imageButtonPatients);
				Animation rotate1 = AnimationUtils.loadAnimation(
						MainActivity.this, R.animator.wave_scale);

				rotate1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						final Intent openPatients = new Intent(
								MainActivity.this, PatientsActivity.class);
						openPatients.putExtra("Screen", 1);
						startActivity(openPatients);
						overridePendingTransition(R.animator.toright,
								R.animator.toleft);

						// openPatients.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// openPatients
						// .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// ActivitySwitcher
						// .animationOut(
						// findViewById(R.id.Totaal),
						// getWindowManager(),
						// new ActivitySwitcher.AnimationFinishedListener() {
						// @Override
						// public void onAnimationFinished() {
						// startActivity(openPatients);
						// }
						// });
					}
				});
				btnPatients.startAnimation(rotate1);
				// MainActivity.this.startActivity(openPatients);
			}
		});

		ImageButton btnScreeningsOverview = (ImageButton) findViewById(R.id.imageButtonScreeningsOverview);
		btnScreeningsOverview.setLayoutParams(new LinearLayout.LayoutParams(p));
		btnScreeningsOverview.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageButton btnScreeningsOverview = (ImageButton) findViewById(R.id.imageButtonScreeningsOverview);
				// Animation rotateimage =
				// AnimationUtils.loadAnimation(MainActivity.this,
				// R.animator.alpha);
				Animation hyperspace = AnimationUtils.loadAnimation(
						MainActivity.this, R.animator.wave_scale);
				// Log.w("aaaaaaaaaaaaaaa" + getWidth(), "" + getHeight());

				hyperspace.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						final Intent openScreeningsOverview = new Intent(
								MainActivity.this,
								ScreeningSummaryActivity.class);
						startActivity(openScreeningsOverview);
						overridePendingTransition(R.animator.toright, R.animator.toleft);
						// openScreeningsOverview
						// .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// ActivitySwitcher
						// .animationOut(
						// findViewById(R.id.Totaal),
						// getWindowManager(),
						// new ActivitySwitcher.AnimationFinishedListener() {
						// @Override
						// public void onAnimationFinished() {
						// startActivity(openScreeningsOverview);
						// }
						// });

					}
				});
				btnScreeningsOverview.startAnimation(hyperspace);
			}
		});

		ImageButton btnSync = (ImageButton) findViewById(R.id.imageButtonSync);
		btnSync.setLayoutParams(new LinearLayout.LayoutParams(p));
		btnSync.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageButton btnNewScreening = (ImageButton) findViewById(R.id.imageButtonSync);
				// Animation rotateimage =
				// AnimationUtils.loadAnimation(MainActivity.this,
				// R.animator.alpha);
				Animation rotate1 = AnimationUtils.loadAnimation(
						MainActivity.this, R.animator.wave_scale);

				rotate1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						final Intent openSync = new Intent(MainActivity.this,
								SyncActivity.class);
						startActivity(openSync);
						overridePendingTransition(R.animator.toright, R.animator.toleft);
						// openSync.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// ActivitySwitcher
						// .animationOut(
						// findViewById(R.id.Totaal),
						// getWindowManager(),
						// new ActivitySwitcher.AnimationFinishedListener() {
						// @Override
						// public void onAnimationFinished() {
						// MainActivity.this
						// .startActivity(openSync);
						// }
						// });
						
						MainActivity.this.finish();
					}
				});
				btnNewScreening.startAnimation(rotate1);
			}
		});

		ImageButton btnLogout = (ImageButton) findViewById(R.id.imageButtonLogout);
		btnLogout.setLayoutParams(new LinearLayout.LayoutParams(p));
		btnLogout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageButton btnLogout = (ImageButton) findViewById(R.id.imageButtonLogout);
				Animation rotate1 = AnimationUtils.loadAnimation(
						MainActivity.this, R.animator.wave_scale);

				rotate1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						final Intent loginAct = new Intent(MainActivity.this,
								LoginActivity.class);
						startActivity(loginAct);
						overridePendingTransition(R.animator.toright, R.animator.toleft);
//						loginAct.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//
//						ActivitySwitcher
//								.animationOut(
//										findViewById(R.id.Totaal),
//										getWindowManager(),
//										new ActivitySwitcher.AnimationFinishedListener() {
//											@Override
//											public void onAnimationFinished() {
//												MainActivity.this
//														.startActivity(loginAct);
//											}
//										});
					}

				});
				btnLogout.startAnimation(rotate1);

			}
		});

		int textsize = getWidth() * 7 / 400;
		Typeface tf = Typeface.createFromAsset(getAssets(), "com.ttf");
		TextView text1 = (TextView) findViewById(R.id.textNewScreening);
		text1.setTypeface(tf, Typeface.BOLD);
		text1.setText("Nieuwe screening");
		text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		TextView text2 = (TextView) findViewById(R.id.textScreeningsOverview);
		text2.setTypeface(tf, Typeface.BOLD);
		text2.setText("Overzicht screenings");
		text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		TextView text3 = (TextView) findViewById(R.id.textPatients);
		text3.setTypeface(tf, Typeface.BOLD);
		text3.setText("Bewoners");
		text3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		TextView text4 = (TextView) findViewById(R.id.textSync);
		text4.setTypeface(tf, Typeface.BOLD);
		text4.setText("Synchroniseren");
		text4.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		TextView text5 = (TextView) findViewById(R.id.textLogout);
		text5.setTypeface(tf, Typeface.BOLD);
		text5.setText("Afmelden");
		text5.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		// LinearLayout.LayoutParams pp = new LinearLayout.LayoutParams(
		// LinearLayout.LayoutParams.WRAP_CONTENT,
		// LinearLayout.LayoutParams.WRAP_CONTENT);
		// int heightButton=(int)((float)getHeight() * 0.75f);
		// p.height = heightButton;
		// p.width = (getWidth() / 5) - ((getWidth()/5)/10);
		//

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	// @Override
	// protected void onResume() {
	// // animateIn this activity
	// ActivitySwitcher.animationIn(findViewById(R.id.Totaal),
	// getWindowManager());
	// super.onResume();
	// }

	// @SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	public void getScreenDimensions() {

		if (android.os.Build.VERSION.SDK_INT >= 13) {

			Display display = getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			width = size.x;
			height = size.y;
			
		} else {
			Display display = getWindowManager().getDefaultDisplay();

			width = display.getWidth(); // deprecated
			height = display.getHeight(); // deprecated
		}

	}

}
